<?php namespace App\Http\Controllers\Admin;

use App\Events\FinishOperation;
use App\Http\Controllers\Controller;
use App\Http\Requests;
use App\Http\Requests\AdminCreateRequest;
use App\Http\Requests\AdminUpdateRequest;
use App\Models\User;
use App\Models\Role;


class AdminController extends Controller {

	public function __construct(User $user)
	{
		parent::__construct();
		$this->user = $user;
	}
	/**
	 * Display a listing of the resource.
	 *
	 * @return Response
	 */
	public function index()
	{
		$list = $this->user->paginate(10);
		return view('admin/admin/index', compact('list'));
	}

	/**
	 * Show the form for creating a new resource.
	 *
	 * @return Response
	 */
	public function create()
	{
		return view('admin/admin/create');
	}

	/**
	 * Store a newly created resource in storage.
	 *
	 * @return Response
	 */
	public function store(AdminCreateRequest $request)
	{
		$data = [
			'name' => $request->input('name'),
			'password' => bcrypt($request->input('password')),
			'email' => $request->input('email'),
		];

		$this->user->create($data);

		return redirect('/admin/admin/index');
	}

	/**
	 * Show the form for editing the specified resource.
	 *
	 * @param  int  $id
	 * @return Response
	 */
	public function edit($id)
	{
		$user = $this->user->find($id);
		return view('admin/admin/edit', compact('user'));
	}

	/**
	 * Update the specified resource in storage.
	 *
	 * @param  int  $id
	 * @return Response
	 */
	public function update(AdminUpdateRequest $request, $id)
	{
		$user = $this->user->find($id);
		$user->name = $request->input('name');

		if($request->input('password'))
			$user->password = bcrypt($request->input('password'));

		$user->email = $request->input('email');

		$user->save();

		return redirect('/admin/admin/index');
	}

	/**
	 * Remove the specified resource from storage.
	 *
	 * @param  int  $id
	 * @return Response
	 */
	public function destroy($id)
	{
		$this->user->find($id)->delete();
		return redirect('/admin/admin/index');
	}

    public function selectPower()
    {
        $list = Role::where('role_pid', '=', 0)->get();

        return view('admin/admin/select_power', compact('list'));
    }
}
